package com.qx.ddx.activity.fragment.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qx.ddx.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/7/25.
 */
public class ThemeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
    private int mSelectedIndex = 0;

    public ThemeAdapter() {
    }

    public void setSelectedIndex(int index) {
        mSelectedIndex = index;
    }

    public void setData(List<HashMap<String, String>> listItems) {
        data = listItems;
    }

    public HashMap<String, String> getItem(int position) {
        return data.get(position);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_theme, null);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        HashMap<String, String> map = getItem(position);
        ((ItemViewHolder) holder).txt.setText(map.get("name"));
        ((ItemViewHolder) holder).bg.setBackgroundResource(Integer.valueOf(map.get("bg")));
        if (map.get("isSelected").equals("true"))
            ((ItemViewHolder) holder).img.setVisibility(View.VISIBLE);
        else
            ((ItemViewHolder) holder).img.setVisibility(View.GONE);
        ((ItemViewHolder) holder).bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelectedIndex == position) {
                    return;
                }
                HashMap<String, String> nowmap = getItem(position);
                int themeBg = Integer.valueOf(nowmap.get("bg"));
                nowmap.put("isSelected", "true");
                data.set(position, nowmap);
                HashMap<String, String> map = getItem(mSelectedIndex);
                map.put("isSelected", "false");
                data.set(mSelectedIndex, map);
                mSelectedIndex = position;
                mItemListener.onItemClick(position, themeBg);
                ThemeAdapter.this.notifyDataSetChanged();
            }
        });
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        public TextView txt;
        public ImageView img;
        View bg;

        public ItemViewHolder(View view) {
            super(view);
            txt = (TextView) view.findViewById(R.id.item_theme_txt);
            img = (ImageView) view.findViewById(R.id.item_theme_img);
            bg = view.findViewById(R.id.item_theme_bg);
        }
    }

    private onItemClickListener mItemListener = null;

    public void setOnItemClickListener(onItemClickListener listener) {
        mItemListener = listener;
    }

    public interface onItemClickListener {
        void onItemClick(int index, int bgId);
    }
}
